package com.sphd.emanu.activity.todaysale;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sphd.emanu.R;
import com.sphd.emanu.adapter.MyViewPagerAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.fragment.saleorder.SaleorderFragment;
import com.sphd.emanu.fragment.topuporder.TopuporderFragment;
import com.sphd.emanu.fragment.topuptodayorder.TopuptodayorderFragment;
import com.sphd.emanu.interfaces.SaleorderFragmentLetter;
import com.sphd.emanu.interfaces.TopuptodayorderFragmentLetter;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.pojo.DaySale;
import com.sphd.emanu.pojo.login.User;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 今日销售额
 *
 * @Author Hu
 * @Time 2017/4/5 -- 14:37.
 * @Email 87683202@qq.com
 */
public class TodaysaleActivity extends MVPBaseActivity<TodaysaleContract.View, TodaysalePresenter> implements TodaysaleContract.View, ViewPager.OnPageChangeListener, TopuptodayorderFragmentLetter, SaleorderFragmentLetter {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.today_sale)
    TextView todaySale;
    @BindView(R.id.title_sale_order)
    TextView titleSaleOrder;
    @BindView(R.id.title_sale_order_no)
    TextView titleSaleOrderNo;
    @BindView(R.id.layout_sale)
    LinearLayout layoutSale;
    @BindView(R.id.title_topup_order)
    TextView titleTopupOrder;
    @BindView(R.id.title_topup_order_no)
    TextView titleTopupOrderNo;
    @BindView(R.id.layout_topup)
    LinearLayout layoutTopup;
    @BindView(R.id.my_vp)
    ViewPager myVp;
    List<Fragment> lists;
    SaleorderFragment saleFragment;
    TopuptodayorderFragment topupFragment;
    MyViewPagerAdapter myViewPagerAdapter;
    String[] titles;
    User user;
    Bundle bundle;
    Intent intent;
    String date=null;
    boolean isSaleOk=false;
    boolean isTopUpOk=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_todaysale);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
        myVp.addOnPageChangeListener(this);
    }

    private void initView() {
        setTitleBar(toolbar,getString(R.string.today_sale));
        intent=getIntent();
        bundle=intent.getExtras();
        user= (User) bundle.getSerializable(Constant.USER_ENTITY);
        date=bundle.getString(Constant.DAY_SALE);
        initFragments();
        addData();
    }

    private void addData() {
        showProgressDialog();
        mPresenter.saleDaySum(user.getOid(),date);
    }

    /**
     * 初始化两个页面
     */
    private void initFragments() {
        titles= new String[]{"销售订单\n0","充值订单\n0"};
        lists=new ArrayList<>();
        saleFragment=new SaleorderFragment();
        saleFragment.setArguments(bundle);
        topupFragment=new TopuptodayorderFragment();
        saleFragment.setSaleorderFragmentLetter(this);
        topupFragment.setTopuptodayorderFragmentLetter(this);
        lists.add(saleFragment);
        lists.add(topupFragment);
        initAdapter();
    }

    /**
     * 出事话适配器
     */
    private void initAdapter() {
        myViewPagerAdapter=new MyViewPagerAdapter(this,getSupportFragmentManager(),lists, titles);
        myVp.setAdapter(myViewPagerAdapter);
        myVp.setCurrentItem(0);
    }

    @OnClick({R.id.layout_sale, R.id.layout_topup})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_sale:
                myVp.setCurrentItem(0);
                break;
            case R.id.layout_topup:
                myVp.setCurrentItem(1);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        select(position);
    }

    /**
     * @param position
     *  设定顶部type的字体颜色
     */
    private void select(int position) {
        initColor();
        switch (position){
            case 0:
                initColorGray(titleSaleOrder,R.color.colorPrimary);
                initColorGray(titleSaleOrderNo, R.color.colorPrimary);
                break;
            case 1:
                initColorGray(titleTopupOrder, R.color.colorPrimary);
                initColorGray(titleTopupOrderNo, R.color.colorPrimary);
                break;

        }
    }

    /**
     * 初始化所有的颜色
     */
    private void initColor() {
        initColorGray(titleSaleOrder,R.color.gray);
        initColorGray(titleSaleOrderNo, R.color.gray);
        initColorGray(titleTopupOrder, R.color.gray);
        initColorGray(titleTopupOrderNo, R.color.gray);
    }

    /**
     * @param textView
     * @param my_gray 设置字体的共用方法
     */
    private void initColorGray(TextView textView, int my_gray) {
        textView.setTextColor(ContextCompat.getColor(this, my_gray));
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void recBean(DaySale daySale) {
        dissProgressDialog();
        todaySale.setText(daySale.getDaySaleSum().toString());
        titleSaleOrderNo.setText(daySale.getOrderSum().toString());
        titleTopupOrderNo.setText(daySale.getDepositSum().toString());
        //在页面Ui加载完毕收
        if (isTopUpOk){
            topupFragment.setList(daySale.getMemDepositList());
        }else {
            toast("充值订单界面加载失败，请稍后重试。");
        }
        if (isSaleOk){
            saleFragment.setList(daySale.getOrderList());
        }else {
            toast("销售订单数据加载失败，请稍后重试。");
        }

    }

    @Override
    public void readyedTopUpToday() {
        isTopUpOk=true;
    }

    @Override
    public void readyedSale() {
        isSaleOk=true;
    }
}
